/**
 * 
 */
package cn.terrence.myoauth2.client.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthResourceResponse;
import org.apache.oltu.oauth2.common.OAuth;

import cn.terrence.myoauth2.common.Constant;

/**
 * @author cuihua022
 * 资源请求Servlet 
 */
public class ResourceGetterServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5156266234238668239L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try{
			String type = request.getParameter("type");
			String accessToken = request.getParameter("accessToken");
			switch(type){
			case "GET":
				get(request,response,type,accessToken);
				break;
			case "POST":
				post(request,response,type,accessToken);
				break;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	
	private void get(HttpServletRequest request, HttpServletResponse response,String type,String accessToken) throws ServletException, IOException{
      try{
		  OAuthClient resourceRequestClient = new OAuthClient(new URLConnectionClient());
	      OAuthClientRequest bearerClientRequest = new OAuthBearerClientRequest(Constant.TEST_SERVER_URL+"/buildResource?id=123")
	      										.setAccessToken(accessToken)
	      										.buildHeaderMessage();
	      OAuthResourceResponse resourceResponse = resourceRequestClient.resource(bearerClientRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
	      String resource = resourceResponse.getBody();
	      request.setCharacterEncoding("UTF-8");
          response.setContentType("text/html;charset=utf-8");
	      request.setAttribute("resource",resource);
	      request.getRequestDispatcher("/WEB-INF/pages/resourceShower.jsp").forward(request, response);
      }catch(Exception ex){
    	  ex.printStackTrace();
      }	
	}
	
	private void post(HttpServletRequest request, HttpServletResponse response,String type,String accessToken) throws ServletException, IOException{
		try{
			OAuthClient resourceRequestClient = new OAuthClient(new URLConnectionClient());
			OAuthClientRequest bearerClientRequest = new OAuthBearerClientRequest(Constant.TEST_SERVER_URL+"/buildResource")
	        									  .setAccessToken(accessToken)
	                                              .buildHeaderMessage();
			//上传文件
			//bearerClientRequest.setHeader(OAuth.HeaderType.CONTENT_TYPE, "multipart/form-data");
			//提交数据
			bearerClientRequest.setHeader(OAuth.HeaderType.CONTENT_TYPE, "application/x-www-form-urlencoded");
			bearerClientRequest.setBody("id=1&name=2&address=England");
			OAuthResourceResponse resourceResponse = resourceRequestClient.resource(bearerClientRequest, OAuth.HttpMethod.POST, OAuthResourceResponse.class);
			String resource = resourceResponse.getBody();
			request.setCharacterEncoding("UTF-8");
	        response.setContentType("text/html;charset=utf-8");
			request.setAttribute("resource",resource);
			request.getRequestDispatcher("/WEB-INF/pages/resourceShower.jsp").forward(request, response);
		}catch(Exception ex){
			ex.printStackTrace();
	    }
	}
}
